package top.stmo.stmoBlog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.stmo.stmoBlog.dao.mapper.CommentMapper;
import top.stmo.stmoBlog.dao.pojo.Comment;
import top.stmo.stmoBlog.dao.vo.CommentVo;
import top.stmo.stmoBlog.dao.vo.UserVo;
import top.stmo.stmoBlog.service.CommentService;
import top.stmo.stmoBlog.service.UserService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserService userService;

    @Override
    public List<CommentVo> findAllComment(Long id) {

        /**
         *  1.根据文章id 查询 评论列表 ，从comment表中查询
         *  2.根据作者id 查询作者信息
         *  3.如果 level = 1 查询有没有子评论
         *  4.如果有子评论，根据id 进行查询（parent_id）
         */
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Comment::getArticleId,id);
        queryWrapper.eq(Comment::getLevel,1);
        List<Comment> commentList = commentMapper.selectList(queryWrapper);
        List<CommentVo> commentVoList = copyList(commentList);

        return commentVoList;
    }

    @Override
    public List<CommentVo> findAllMessage() {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Comment::getArticleId,0);
        queryWrapper.eq(Comment::getLevel,1);
        List<Comment> commentList = commentMapper.selectList(queryWrapper);
        List<CommentVo> commentVoList = copyList(commentList);
        return commentVoList;
    }

    private List<CommentVo> copyList(List<Comment> commentList) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        //创建时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String str = dateFormat.format(comment.getCreateDate());
        commentVo.setCreateDate(str);
        //评论者信息
        UserVo userVo = userService.findUserById(comment.getUserId());
        commentVo.setUser(userVo);
        //子评论
        Integer level = comment.getLevel();
        if(1 == level){
            Long id = comment.getId();
            List<CommentVo> commentVoList = findCommentsByParentId(id);
            commentVo.setChildrens(commentVoList);
        }
        //被评论者信息
        if(level > 1) {
            UserVo toUserVo = userService.findUserById(comment.getToUserId());
            commentVo.setToUser(toUserVo);
        }

        return commentVo;
    }

    private List<CommentVo> findCommentsByParentId(Long id) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Comment::getParentId,id);
        queryWrapper.eq(Comment::getLevel,2);
        return copyList(commentMapper.selectList(queryWrapper));
    }
}
